﻿
using MesBSW.BLL.Role;
using MesBSW.DAL.RoleDB;
using MesBSW.Tools;
using MesBSW.View.Dialog.Role;
using Sunny.UI;
using System;
using System.Linq;
using System.Windows.Forms;

namespace MesBSW.View.RolePages
{
    public partial class DeptFrm : UIPage
    {

        private DeptInfoControler _my_ctrl = new DeptInfoControler();
        public DeptFrm()
        {
            InitializeComponent();
        }

        private void InitDeptTreeView() 
        {
            this.treeViewDept.Nodes.Clear();
            var menus = _my_ctrl.getDeptTreeNodes();
            TreeNode TopNode = new TreeNode("部门", menus.ToArray());
            TopNode.Toggle();
            this.treeViewDept.Nodes.Add(TopNode);
        }


        private void InitDeptDgv() 
        {
            dgvDept.Init();
            dgvDept.AddColumn("ID", "ID").Visible=false;
            dgvDept.AddColumn("dept_code", "dept_code").HeaderText="部门编号";
            dgvDept.AddColumn("dept_name", "dept_name").HeaderText = "部门名称";
            dgvDept.AddColumn("create_user", "create_user").HeaderText = "创建人";
            InitDeptDatas();
        }

        private void InitDeptDatas() 
        {
            dgvDept.DataSource = _my_ctrl.getDepts().ToArray();
        }

        private void btnAddDept_Click(object sender, EventArgs e)
        {
            AddDeptDia addDeptDia = new AddDeptDia(_my_ctrl);
            addDeptDia.Render();
            addDeptDia.ShowDialog();
            if (addDeptDia.IsOK)
            {
                RefreshData();
            }
        }

        private void treeViewDept_AfterSelect(object sender, TreeViewEventArgs e)
        {
            dgvDept.DataSource = _my_ctrl.getDepts(e.Node.Name.ToInt()).ToArray();
        }

        private void dgvdept_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            BaseDeptModel deptModel = new BaseDeptModel();
            deptModel.id = dgvDept.Rows[e.RowIndex].Cells["ID"].Value.ToString().ToInt();
            deptModel = _my_ctrl.getDept(deptModel.id);
            AddDeptDia addDeptDia = new AddDeptDia(_my_ctrl, deptModel);
            addDeptDia.Render();
            addDeptDia.ShowDialog();
            if (addDeptDia.IsOK)
            {
                RefreshData();
            }
        }

        private void btndeldept_Click(object sender, EventArgs e)
        {
            if (dgvDept.SelectedRows.Count > 0)
            {
                int id = dgvDept.Rows[dgvDept.SelectedIndex].Cells["ID"].Value.ToString().ToInt();
                _my_ctrl.delDept(id);
                RefreshData();
                UIMessageBox.Show(SysTools.errMsg);
            }
            else
            {
                UIMessageBox.ShowWarning("请在右边列表中选择部门后再删除！");
            }
            
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void RefreshData() 
        {
            InitDeptTreeView();
            InitDeptDatas();
        }

        private void DeptFrm_Load(object sender, EventArgs e)
        {
            InitDeptTreeView();
            InitDeptDgv();
        }
    }
}
